import 'package:flutter/material.dart';

///
/// 通用文本搜索框
///
class QCommonSearchTextField extends StatelessWidget {
  final ValueChanged<String>? onSubmitted;
  final VoidCallback? onTab;
  final String hintText;
  final EdgeInsetsGeometry? margin;

  const QCommonSearchTextField({
    Key? key,
    this.hintText = "搜索",
    this.margin,
    this.onSubmitted,
    this.onTab,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin ?? const EdgeInsets.all(0.0),
      width: MediaQuery.of(context).size.width,
      alignment: AlignmentDirectional.center,
      height: 37.0,
      decoration: BoxDecoration(
          color: const Color.fromARGB(255, 237, 236, 237),
          borderRadius: BorderRadius.circular(24.0)),
      child: TextField(
        onSubmitted: (value) {
          if (onSubmitted != null) {
            onSubmitted!.call(value);
          }
        },
        onTap: () {
          if (onTab != null) {
            onTab!.call();
          }
        },
        cursorColor: const Color.fromARGB(255, 0, 189, 96),
        decoration: InputDecoration(
            contentPadding: const EdgeInsets.all(0),
            border: InputBorder.none,
            hintText: hintText,
            hintStyle: const TextStyle(
                fontSize: 17, color: Color.fromARGB(255, 192, 191, 191)),
            prefixIcon: const Icon(
              Icons.search,
              size: 25,
              color: Color.fromARGB(255, 128, 128, 128),
            )),
        style: const TextStyle(fontSize: 17),
      ),
    );
  }

  getContainer(BuildContext context, ValueChanged<String> onSubmitted) {
    return Container(
      width: MediaQuery.of(context).size.width,
      alignment: AlignmentDirectional.center,
      height: 40.0,
      decoration: BoxDecoration(
          color: const Color.fromARGB(255, 237, 236, 237),
          borderRadius: BorderRadius.circular(24.0)),
      child: TextField(
        onSubmitted: onSubmitted,
        cursorColor: const Color.fromARGB(255, 0, 189, 96),
        decoration: InputDecoration(
            contentPadding: EdgeInsets.zero,
            border: InputBorder.none,
            hintText: hintText,
            hintStyle: const TextStyle(fontSize: 20),
            prefixIcon: const Icon(
              Icons.search,
              size: 29,
              color: Color.fromARGB(255, 128, 128, 128),
            )),
        style: const TextStyle(fontSize: 20),
      ),
    );
  }
}
